<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
//require_once('fpdf/fpdf.php');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class BuktiBayar extends FPDF {

    //put your code here
//    'courier'
    var $cetak;
    var $lineh;
    var $hfill;
    var $hheader;
    var $revdata;
    private $logourl;
    private $namasekolah;
    private $alamat;
    private $dataheader;
    private $CI;

    function Header() {
        $this->hheader = 4;
//Logo
        $this->Image($this->logourl, 3, 3, 15);

//Arial bold 15
//        $this->SetY(8);
        $this->SetFont('Arial', 'B', 12);
        $this->SetTextColor(1, 50, 155);
        $this->SetY(8);
        $this->Cell(8);
        $this->Cell(70, 5, $this->namasekolah, 0);
        $this->Ln(5);
        $this->SetFont('Arial', 'B', 8);
        $this->Cell(8);
        $this->Cell(70, 3, $this->alamat);
        $this->SetFont('Arial', 'IB', 14);
        $this->SetY(3);
        $this->Cell(100);
        $this->Cell(100, 16, 'SIMULASI PEMBAYARAN SEKOLAH', 0, 0, 'R');

        $this->Ln(16);
        $data = $this->dataheader;
        $this->SetFont('Arial', '', 8);
        $this->SetX(5);
        $this->Cell(16, $this->hheader, 'NIS ', 'TL', 0, 'L');
        $this->Cell(3, $this->hheader, ':', 'T', 0, 'C');
        $this->Cell(106, $this->hheader, $data->nis, 'TR', 0, 'L');
        $this->Cell(22, $this->hheader, 'Tanggal ', 'TL', 0, 'L');
        $this->Cell(3, $this->hheader, ':', 'T', 0, 'C');
        $this->Cell(55, $this->hheader, $data->tglbayar, 'TR', 0, 'L');
        $this->Ln();
        $this->SetX(5);
        $this->Cell(16, $this->hheader, 'Nama ', 'TLB', 0, 'L');
        $this->Cell(3, $this->hheader, ':', 'TB', 0, 'C');
        $this->Cell(106, $this->hheader, $data->nama, 'TBR', 0, 'L');
        $this->Cell(22, $this->hheader, 'Tahun Ajaran ', 'TLB', 0, 'L');
        $this->Cell(3, $this->hheader, ':', 'TB', 0, 'C');
        $this->Cell(55, $this->hheader, $data->thajaran, 'TBR', 0, 'L');
        $this->Ln();
        $this->SetX(5);
        $this->Cell(16, $this->hheader, 'Keterangan ', 'LB', 0, 'L');
        $this->Cell(3, $this->hheader, ':', 'B', 0, 'C');
        $this->Cell(106, $this->hheader, $data->keterangan, 'BR', 0, 'L');
        $this->Cell(22, $this->hheader, 'Semester/Kelas ', 'BL', 0, 'L');
        $this->Cell(3, $this->hheader, ':', 'B', 0, 'C');
        $this->Cell(55, $this->hheader, $data->semester . ' / ' . $data->kelas, 'BR', 0, 'L');
//        $this->Ln();
        $this->SetY(27);
//        31
        $this->set_column_header();
        $this->Ln();
    }

    public function setUrlLogo($url) {
        $this->logourl = $url;
    }

    public function setNamasekolah($value) {
        $this->namasekolah = $value;
    }

    public function setAlamat($value) {
        $this->alamat = $value;
    }

    public function setDataheader($value) {
        $this->dataheader = $value;
    }

    public function getDetail_thbl($detail, $thbl, $kd_biaya) {
        $arr = array();
        foreach ($detail as $v) {
            if ($v->thbl == $thbl && $v->kd_biaya == $kd_biaya) {
                array_push($arr, $v);
//                $arr= $v;
            }
        }
        return $arr;
    }

    public function set_column_header() {
        $header = array('Jenis Pembayaran', 'Biaya Dikenakan', 'Potongan',
            'Total Biaya', 'Cicilan', 'Saldo', 'Jumlah Bayar');
        $colwidth = array(55, 25, 25, 25, 25, 25, 25);
        $alignheader = array('C', 'R', 'R', 'R', 'R', 'R', 'R');
        $borderheader = array('LB', 'B', 'B', 'B', 'B', 'B', 'BR');
        $this->Ln();
        $this->SetX(5);
        for ($w = 0; $w < count($colwidth); $w++) {
//                $this->Cell($colwidth[$w], $this->hheader, $header[$w], 1, 0, $alignheader[$w]);
            $this->Cell($colwidth[$w], $this->hheader, $header[$w], $borderheader[$w], 0, $alignheader[$w]);
        }
//            $this->sety(-6);
//            $this->Cell($colwidth[0], $this->hfill, $header[0], $borderheader[0], 0, $alignheader[0]);
    }

    public function create_pdf($detail, $arrthbl, $masterbiaya) {
//        $data=array('nis'=>'12345678910123456789','nama'=>'Faroq Agus Riyadi','kelas'=>'Kelas I B'
//            ,'semester'=>'I','thajaran'=>'2014/2015','tglbayar'=>date('d F Y'),'keterangan'=>'llllllll');
        $this->hfill = 4;
        $this->SetDrawColor(1, 50, 155);
        $this->SetTextColor(1, 50, 155);
        $this->SetFont('Arial', '', 8);
        $this->AddPage('L');
        $totalall = 0;
        foreach ($arrthbl as $vthbl) {


            $this->Rect(5, 35, 205, 44);
//            $this->Rect(130, 31, 80, 62);
            $header = array('Jenis Pembayaran', 'Biaya Dikenakan', 'Potongan',
                'Total Biaya', 'Cicilan', 'Saldo', 'Jumlah Bayar');
            $colwidth = array(55, 25, 25, 25, 25, 25, 25);
            $alignheader = array('C', 'R', 'R', 'R', 'R', 'R', 'R');
//            $this->Ln();
//            $this->SetX(5);
//            for ($w = 0; $w < count($colwidth); $w++) {
//                $this->Cell($colwidth[$w], $this->hfill, $header[$w], 'B', 0, $alignheader[$w]);
//            }
            $groupdetail = '';
//            $this->Ln();

            $this->SetFont('Arial', 'B', 8);
            $this->SetX(5);
            $this->Cell($colwidth[0], $this->hfill, 'Periode - ' . $vthbl, 'BT', 0, 'L');

            $this->Ln();


            $nomor = 0;
            foreach ($masterbiaya as $v) {
                $y = $this->GetY();
                if ($y > 71) {
                    $this->AddPage('L');
                    $this->Rect(5, 35, 205, 44);
//                    $this->Ln();
                }
//            $this->SetY($y+4);
                if ($groupdetail !== $v->jenis_keterangan) {
                    $groupdetail = $v->jenis_keterangan;
                    $nomor = 0;
                    $this->SetFont('Arial', 'B', 8);
                    $this->SetX(5);
                    $this->Cell(125, $this->hfill, $v->jenis_keterangan . ' Rekening : ' . $v->no_rek, 0, 0, 'L');
                    $this->Ln();
                }
                $nomor++;
                $this->SetFont('Arial', '', 8);
                $this->SetX(5);
                $this->Cell($colwidth[0], $this->hfill, $nomor . '-' . $v->keterangan, 0, 0, 'L');
                $arr = $this->getDetail_thbl($detail, $vthbl, $v->kd_biaya);
                if (count($arr) > 0) {
                    $this->Cell($colwidth[1], $this->hfill, number_format($arr[0]->biaya_dikenakan), 0, 0, 'R');
                    $this->Cell($colwidth[2], $this->hfill, number_format($arr[0]->total_potongan), 0, 0, 'R');
                    $this->Cell($colwidth[3], $this->hfill, number_format($arr[0]->total_biaya), 0, 0, 'R');
                    $this->Cell($colwidth[4], $this->hfill, number_format($arr[0]->pelunasan), 0, 0, 'R');
                    $this->Cell($colwidth[5], $this->hfill, number_format($arr[0]->saldo), 0, 0, 'R');
                    $this->Cell($colwidth[6], $this->hfill, number_format($arr[0]->jumlah_bayar), 0, 0, 'R');
                    $totalall = $totalall + $arr[0]->jumlah_bayar;
                } else {
                    $this->Cell($colwidth[1], $this->hfill, number_format(0), 0, 0, 'R');
                    $this->Cell($colwidth[2], $this->hfill, number_format(0), 0, 0, 'R');
                    $this->Cell($colwidth[3], $this->hfill, number_format(0), 0, 0, 'R');
                    $this->Cell($colwidth[4], $this->hfill, number_format(0), 0, 0, 'R');
                    $this->Cell($colwidth[5], $this->hfill, number_format(0), 0, 0, 'R');
                    $this->Cell($colwidth[6], $this->hfill, number_format(0), 0, 0, 'R');
                }
                $this->Ln();
            }
//            $this->Cell(125, $this->hfill,'Delapan Juta Delapan Ratus Delapan Puluh Ribu Rupiah ', 1, 0, 'L');
        }
//        $this->SetLineWidth(.5);
//        $this->Line(5, 20, 210, 20);
        $y = $this->GetY();

//        $this->SetLineWidth(.25);
//        $this->Line(5, $y+4, 210, $y+4);


        if ($y + 5 > 71) {
            $this->AddPage('P');
            $this->Rect(5, 35, 205, 44);
        }

        $this->SetY(71);
        $this->SetX(7);
        $this->Cell(15, 6, 'Terbilang', 1, 0, 'L');
        $this->SetFont('Arial', 'I', 8);

        $this->CI = & get_instance();
        $this->CI->load->helper('function_terbilang');

        $this->Cell(108, 6, ucwords(terbilang($totalall)), 'TBR', 0, 'L');

        $this->SetFont('Arial', '', 8);
        $this->Cell(22, 6, 'Total Bayar', 'TBR', 0, 'C');
        $this->SetFont('Arial', 'I', 8);
        $this->Cell(56, 6, number_format($totalall), 'TBR', 0, 'R');
    }

}

?>
